/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import db.DBAccess;
import entity.Student;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DucND
 */
public class StudentDAO {
    
    public static void example() {
        Connection conn = DBAccess.getConnection();
    }
    
    public static void delete(String id){
         try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con =
                    DriverManager.getConnection("jdbc:odbc:SMS");
            PreparedStatement prst =
                    con.prepareStatement("Delete from Student where StudentID = '"+id+"'");
            prst.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(StudentDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(StudentDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void edit(Student stu) throws ClassNotFoundException, SQLException{
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = 
                    DriverManager.getConnection("jdbc:odbc:SMS");
            PreparedStatement prst =
                    con.prepareStatement("Update Student set Name = '"+stu.getName()+"', Age = "+stu.getAge()+", Address = '"+stu.getAddress()+"', Gender = '"+stu.getGender()+"', Batch = '"+stu.getBatch()+"', Image = '"+stu.getImage()+"' where StudentId = '"+stu.getStudentID()+"'");
            prst.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(StudentDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(StudentDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static Student getStudent(String Id){
        Student stu= new Student();
        
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:SMS");
            PreparedStatement prst =
                    con.prepareStatement("select * from Student where StudentID = '"+Id+"'");
            ResultSet rs = prst.executeQuery();
            rs.next();
            stu.setStudentID(rs.getString("StudentID"));
            stu.setName(rs.getString("Name"));
            stu.setAge(rs.getInt("Age"));
            stu.setAddress(rs.getString("Address"));
            stu.setGender(rs.getString("Gender").charAt(0));
            stu.setBatch(rs.getString("Batch"));
            stu.setImage(rs.getString("Image"));
        } catch (SQLException ex) {
            Logger.getLogger(StudentDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(StudentDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return stu;
     }
}
